<?php
// +----------------------------------------------------------------------
// | MYUPHP [ Simple and practical framework ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-08-10 http://myucms.com All rights reserved.
// +----------------------------------------------------------------------
// | Copyright (c) 2020-08-10 http://115cms.com All rights reserved.
// +----------------------------------------------------------------------
// | Copyright (c) 2020-08-10 http://guojiz.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
require PATH . '/Common.php';
use myuphp\Common\Db;
use myuphp\common\Myuphp;
use myuphp\common\Session;
// 通用过滤
if (!function_exists('pre')) {
    function pre($str,$pre='')
    {
        if ($pre) {
            $new = preg_replace("/".$pre."/is", "", $str);
        } else {
            $new = preg_replace("/<.*?>|<\\/>/is", "", $str);
        }
        return $new;
    }
}
//定义值
if (!function_exists('dingyi')) {
    function dingyi($s,$v='1'){
        if (empty($s)) {
            return $v;
        } else {
            return $s;
        }
    }
}
if (!function_exists('get')) {
    // 获取提交数据带过滤
    function get($get = 'c', $id = '0', $pre = '')
    {
        $v = preg_replace("/(.*)".$get."/",$get,$_SERVER['REQUEST_URI']);
        $v = preg_replace("/\//",Myuphp::os_config('path'),$v);
        $s =explode(Myuphp::os_config('path'), $v);
        if ($s[0]==$get) {
            return pre($s[1],dingyi($pre,'.html'));
        } else {
            if (!empty($_REQUEST[$get])) {
                return pre($_REQUEST[$get]);
            } else {
                return pre($id,$pre);
            }
        }

    }
}
if (!function_exists('input')) {
    // 获取提交不带过滤
    function input($get = 'c', $id = '0')
    {
        $v = preg_replace("/(.*)".$get."/",$get,$_SERVER['REQUEST_URI']);
        $v = preg_replace("/\//",Myuphp::os_config('path'),$v);
        $s =explode(Myuphp::os_config('path'), $v);
        if ($s[0]==$get) {
            return pre($s[1],dingyi($pre,'.html'));
        } else {
            if ($get == 'post.') {
                return $_POST;
            } else {
                if (!empty($_REQUEST[$get])) {
                    return $_REQUEST[$get];
                } else {
                    return $id;
                }
            }
 
        }
    }
}
if (!function_exists('dt')) {
    // 默认值s，m，c
    function dt($get)
    {
        if (Myuphp::server()=='/') {
            $str=preg_replace('/\?/','',$_SERVER['REQUEST_URI'],1);
            $str=preg_replace("/\/s=/","",$str,1);
            $str=preg_replace("/&m=/",",",$str,1);
            $str=preg_replace("/&c=/",",",$str,1);
            $str=preg_replace("/&(.*)|\?(.*)/","",$str,1);
        } else {
            $str=preg_replace('/\?/','',Myuphp::server(),1);
            $str=preg_replace("/\//","",$str,1);
            $str=preg_replace("/\//",Myuphp::os_config('path'),$str);
            if (Myuphp::os_config('path')=='/') {
                $str=preg_replace("/\//",",",$str,1);
                $str=preg_replace("/\//",",",$str,1);
                $str=preg_replace("/\/(.*)/","",$str);
            } else {
                $str=preg_replace("/".Myuphp::os_config('path')."/",",",$str,1);
                $str=preg_replace("/".Myuphp::os_config('path')."/",",",$str,1);
                $str=preg_replace("/".Myuphp::os_config('path')."(.*)/","",$str);
            }
        }
        $name = array('s','m','c');
        $v = explode(",", $str);
        $c=array_combine($name,$v);
        return $c[$get];
    }
}
if (!function_exists('server')) {
    function server()
    {
    	$include = (include PATH.'/Route.php');
    	$url= preg_replace('/\\'.Myuphp::os_config('path').'/is', '/', preg_replace('/\\/index.php|.html|\?(.*)/', '', $_SERVER['REQUEST_URI']));
    	$preg= preg_replace('/\?/is', '/', $url);
    	$arr = explode('/', $preg);
    	if (array_search($arr[1],$include)) {
    		$route = array_search($arr[1],$include);
    		return preg_replace('/\\/index.php|.html|\?(.*)/', '', '/'.$route);
    	} else {
    		$route = array_search($arr[1],$include);
    		return preg_replace('/\\/index.php|.html|\?(.*)/', '', $_SERVER['REQUEST_URI']);
    	}
     
    }
}
if (!function_exists('url')) {
    // 默认值s，m，c
    function url($get,$cs='')
    {
 
        $url= preg_replace('/\\'.Myuphp::os_config('path').'/is', '/', $_SERVER["HTTP_HOST"]);
        if (Myuphp::os_config('static')=='true') {
            if ($cs) {
                foreach($cs as $x=>$v)
                {
                   $data .= Myuphp::os_config('path').$x.Myuphp::os_config('path').$v;
                }
            }
            //支持伪静态
            $include = (include PATH.'/Route.php');
            if (array_search($include[$get],$include)) {
                $data=preg_replace("/&/","?",$data,1);
    		    return http().$url.'/'.$include[$get].$data.'.html';
        	} else {
        		return http().$url.'/'.preg_replace("/\//is",Myuphp::os_config('path'), $get).$data.'.html';
        	}
        } else {
            if ($cs) {
                foreach($cs as $x=>$v)
                {
                   $data .= '&'.$x.'='.$v;
                }
            }
            $str='/'.$get;
            $str=preg_replace("/\//","?s=",$str,1);
            $str=preg_replace("/\//","&m=",$str,1);
            $str=preg_replace("/\//","&c=",$str,1);
            return http().$_SERVER["HTTP_HOST"].'/'.$str.$data;
        }
    }
}
if (!function_exists('http')) {
    /*
    http协议自动判断
    */
    function http()
    {
        $http_type = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' ? 'https://' : 'http://';
        return $http_type;
    }
}
if (!function_exists('config')) {
    //获取自定义配置文件数据
    function config($config, $id)
    {
        $include = (include './data/config/' . $config . '.php');
        return $include[$id];
    }
}
if (!function_exists('ck')) {
    //Cookie
    //取值ck('userid');
    //设置值 ck('userid',1);
    //清空 ck('userid',1,1);
    function ck($name, $v = '', $time = 1890662400)
    {
        if (empty($v)) {
            // 取值
            return $_COOKIE[$name];
        } else {
            // 设置值
            return setcookie($name, $v, $time);
        }
    }
}
if (!function_exists('get_url')) {
    //跳转链接
    function get_url($url)
    {
        return header(sprintf('Location:%s', $url));
    }
}
if (!function_exists('curPageURL')) {
    //说明：获取完整URL
    function curPageURL()
    {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {
            $pageURL .= "s";
        }
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["HTTP_HOST"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
        }
        return $pageURL;
    }
}
if (!function_exists('json')) {
    //获取json数据
    function json($array = [])
    {
        echo json_encode($array);
    }
}
if (!function_exists('_post')) {
    //判断是否是post过来的值
    function _post()
    {
        return $_SERVER['REQUEST_METHOD'] == 'POST';
    }
}
if (!function_exists('ps')) {
    function ps($s, $lu = 'luyu')
    {
        $txt = urldecode($s);
        $ch = $txt[0];
        $nh = strpos(APP_MA, $ch);
        if ($lu == 2) {
            $mdKey = md5('luyu' . $ch);
        } else {
            $mdKey = md5($lu . $ch);
        }
        $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
        $txt = substr($txt, 1);
        $tmp = '';
        $i = 0;
        $j = 0;
        $k = 0;
        for ($i = 0; $i < strlen($txt); $i++) {
            $k = $k == strlen($mdKey) ? 0 : $k;
            $j = strpos(APP_MA, $txt[$i]) - $nh - ord($mdKey[$k++]);
            while ($j < 0) {
                $j += 64;
            }
            $tmp .= APP_MA[$j];
        }
        if ($lu == 1) {
            return $s;
        } else {
            return base64_decode($tmp);
        }
    }
}
if (!function_exists('sn')) {
    //创建获取Session1
    function sn($name,$v='',$time='31536000'){
        session_start(); 
        if ($v=='') {
            //取值
            return Session::get($name);
        } elseif($v=='null'){
            //清空
            unset($_SESSION[$name]);  
        } else {
            //设置
            return Session::set($name, $v, $time);  
        }
    }
}
if (!function_exists('session')) {
    //创建获取Session2
    function session($name,$v='',$time='31536000'){
        session_start(); 
        if ($v=='') {
            return $_SESSION[$name];
        } elseif($v=='null'){
            return session_destroy();
        } else {
            setcookie(session_name(),session_id(),time()+$time.'/');  
            return $_SESSION[$name]=$v;
        }
    }
}